'use strict';
/* Controllers */
app.controller('memberInfoCtrl', ['$scope', 'apiService', 'toaster', '$cookieStore', '$rootScope','$window','$timeout', function ($scope, apiService, toaster, $cookieStore, $rootScope,$window,$timeout) {

    var find_obj = {
        memberId: $rootScope.userObj['id'],
        role: 0
    };

    // 获取会员信息
    apiService.post({
        modelName: 'userinfo',
        apiName: 'getUserinfoBymemId'
    }, find_obj, function (data) {
        if (data.code == 200) {
            $scope.userInfo = data['data'][0];
        } else {
            toaster.pop('error', '提示', data['msc']);
        }
    }, function (err) {
        toaster.pop('error', '提示', '服务器出小差咯...');
    });

    // 修改
    $scope.editInfo = function () {
        $scope.btn_disabled = true;
        if ($scope.userInfo['id']) {
            // 更新会员信息
            apiService.post({
                modelName: 'userinfo',
                apiName: 'updateUserinfo'
            }, $scope.userInfo, function (data) {
                $scope.btn_disabled = false;
                if (data.code == 200) {
                    toaster.pop('success', '提示', data['msc']);
                } else {
                    toaster.pop('error', '提示', data['msc']);
                }
            }, function (err) {
                $scope.btn_disabled = false;
                toaster.pop('error', '提示', '服务器出小差咯...');
            });

        } else {
            // 添加会员信息
            $scope.userInfo['role']=0;
            $scope.userInfo['memberId']=$rootScope.userObj['id'];
            apiService.post({
                modelName: 'userinfo',
                apiName: 'addUserinfo'
            }, $scope.userInfo, function (data) {
                $scope.btn_disabled = false;
                if (data.code == 200) {
                    toaster.pop('success', '提示', data['msc']);
                    $timeout(function(){
                        $window.location.reload();
                    },300);
                } else {
                    toaster.pop('error', '提示', data['msc']);
                }
            }, function (err) {
                $scope.btn_disabled = false;
                toaster.pop('error', '提示', '服务器出小差咯...');
            });

        }
    };


}]);